<?php
namespace app\front\logic;
use think\facade\Db;
use app\front\logic\DictLogic;
use app\common\logic\SmsApiLogic;
use app\front\logic\CommonLogic;
use app\front\logic\OrderThirdPayLogLogic;
use app\common\logic\MemberAccountLogic;
use app\common\logic\SmsOrderLogic;


//用户来源操作
class ChannelCountryLogic{

	public static $init;
	static function init(){
		if (!isset(self::$init)) {//判断是否以前创建了当前类的实例
            $c = __CLASS__;//获取类名
            self::$init = new static;//如果没有创建，实例化当前类，这里实现类只实例化一次
        }
        return self::$init;//返回类的实例
	}


	function getList($lang='zh'){
		$continent = input('continent', 0);
		$field = "id,country_ico,country_code,continent";
		$order = "";

		if($lang=='en'){
			$field .= ", country_en as country_name";
			$order = "country_en asc";
		}else{
			$field .= ", country_zh as country_name";
			$order = "country_zh asc";
		}
		if(!$continent){
			$list = DictLogic::init()->getList($lang, 'continent');
			foreach($list as $k=>$v){
				$list[$k]['list'] = Db::name('channel_country')
									->field($field)
									->where('continent', $v['id'])
									->order($order)
									->where('status', 1)
									->select();

			}
			return $list;
		}else{
			$list = Db::name('channel_country')
						->field($field)
						->where('continent', $continent)
						->order($order)
						->where('status', 1)
						->select();
		}
		return $list;
	}


	//获取国家对应的服务列表
	function getServerList($params=[], $info=[]){
		if(!$params['is_ok'] || !$params['country_id']){
			return false;
		}

		$field = "cs.id,ccs.channel_country_id as country_id,cs.server_ico,cs.server_code,ccs.base_rate,1 enbale,0 price,0 o_price,0 counts";
		if($params['lang']=='en'){
			$field .= ", server_en as server_name";
		}else{
			$field .= ", server_zh as server_name";
		}
		
		//获取本系统服务列表
		$lists = Db::name('channel_country_server')
					->alias('ccs')
					->field($field)
					->join('channel_server cs','cs.id=channel_server_id')
					->where('cs.status',1)
					->where('ccs.channel_country_id', $params['country_id'])
					->order('cs.sort asc, cs.server_zh asc')
					->select()
					->toArray();
		// sys_log(json_encode($lists),'guoija.log');
		$all_counts = SmsApiLogic::getAllCounts('get_all_counts', $params['channel_id'], $info);
		//获取api服务列的价格和对应的数量
		$onlineLists = SmsApiLogic::getPricesByCountry('get_prices_by_country', $params['channel_id'], $info);
		$newData = [];
		$nowDate = date('Y-m-d');
		if($lists && $onlineLists){
			foreach($lists as $k=>$v){
				if($params['channel_id']==4){
					if(!isset($onlineLists[$info['country_code']])){

					}
					foreach($onlineLists[$info['country_code']] as $k2=>$v2){
						if($v['server_code'] == $v2['server_code']){
							$lists[$k]['price'] = round($v2['price'] * $v['base_rate'] * $params['item_exchange_rate'],2);
							$lists[$k]['currency_symbol'] = $params['currency_symbol'];
							$lists[$k]['o_price'] = $v2['price'];
							$lists[$k]['counts'] = 100;

							if($v2['price']<=0){
								$lists[$k]['counts'] = 0;
							}
						}
						
                    }
				}elseif($params['channel_id']==3){
					// sys_log("OK\n","guojia.log");
					foreach($onlineLists['services'] as $k2=>$v2){
						// sys_log($v['server_code']."##".$v2['slug']."@@\n","guojia.log");
                        if($v['server_code']==$v2['slug']){
                        	// sys_log("进来了\n","guojia.log");
							$lists[$k]['enable'] = 1;
							//价格先乘以基数
							$lists[$k]['price'] = round($v2['price'] * $v['base_rate'] * $params['item_exchange_rate'],2);
							$lists[$k]['currency_symbol'] = $params['currency_symbol'];
							$lists[$k]['o_price'] = $v2['price'];
							$lists[$k]['counts'] = $v2['count'];
							// sys_log(json_encode($lists[$k]),"guojia.log");
							continue;
						}
                    } 

				}else{
					foreach($onlineLists as $k2=>$v2){
						$lists[$k]['currency_symbol'] = $params['currency_symbol'];
						if($params['channel_id']==2){
							//https://vak-sms.ru/api/vak/
							if($v['server_code']==$k2){
								// echo '<pre>';
								// echo $k2;
								// print_r($v2);
								$lists[$k]['enable'] = 1;
								//价格先乘以基数
								$lists[$k]['price'] = round($v2[0]['cost'] * $v['base_rate'] * $params['item_exchange_rate'],2);
								$lists[$k]['currency_symbol'] = $params['currency_symbol'];
								$lists[$k]['o_price'] = $v2[0]['cost'];
								$lists[$k]['counts'] = $v2[0]['quantity'];
							}
						}else{
							//https://simsms.org/
							if($v['server_code']==$v2['service_opt']){
								$lists[$k]['enable'] = $v2['enable'];
								//价格先乘以基数
								$lists[$k]['price'] = round($v2['price'] * $v['base_rate'] * $params['item_exchange_rate'],2);
								$lists[$k]['o_price'] = $v2['price'];
							}
							foreach($all_counts as $vvv){
								if($vvv['Service']==$v['server_code']){
									$lists[$k]['counts'] = $vvv['Online'];
								}
							}
						}
					}
				}
				// print_r($lists);
				if($lists[$k]['counts']>0){
					$newData[] = $lists[$k];
				}
			}
		}
		// print_r($newData);
		return $newData;
	}

	//获取服务对应的信息
	function getServerInfo($params=[], $info=[], $member_info=[]){
		if(!$member_info || !$member_info['id']){
			return ['code'=>402];
		}
		// print_r($member_info);
		if(!$params['is_ok'] || !$params['country_id'] || !$params['server_id']){
			return ['code'=>100000];
		}
		//查询API账户余额
		$rs = SmsApiLogic::getBalance('get_balance', $info);
		if(!$rs || $rs['response']!=1){
			return ['code'=>144,'data'=>[]];
		}
		//获取服务价格
		$rs2 = SmsApiLogic::getServicePrice('get_service_price', $info);
		if($info['channel_id']==3){
			$tmp = [];
			if($rs2 && isset($rs2['services'])){
				foreach($rs2['services'] as $va){
					$tmp = $va;
					$tmp['response'] = 1;
				}
			}

			$rs2 = $tmp;
			if($rs2['count']<=0){
				return ['code'=>159];
			}
		}else{
			if(!$rs2  || $rs['response']!=1){
				return ['code'=>145,'data'=>[]];
			}
		}
		
		//API账户金额的对比
		if($rs['balance']<$rs2['price']){
			return ['code'=>146,'data'=>[]];
		}
		//查询用户余额
		//服务金额转化成人民币
		$server_price = $rs2['price'] * $params['item_exchange_rate'] * $params['item_base_rate'];

		if($server_price>$params['total_amount']){
			return ['code'=>147,'data'=>[]];
		}
		//开始购买
		//开始生成订单，先扣除用户的金额，然后再通过API来购买
		$od = [];
		$od['member_id']	= 	$member_info['id'];
		$od['channel_id']	= 	$params['channel_id'];
		$od['country_id']	=	$params['country_id'];
		$od['server_id']	=	$params['server_id'];
		$od['country_code'] =	$params['country_code'];
		$od['server_code']	=	$params['server_code'];
		$od['pay_amount']	=	$server_price * $params['exchange_rate'];
		$od['status']		=	10;
		$od['order_sn']		=	CommonLogic::init()->createSn('order_sms','order_sn',14,'number','SO');
		$od['add_time']		=	date('Y-m-d H:i:s');
		// print_r($od);exit;
		//获取api key
		$keys = Db::name('channel_key')
				->where('status',1)
				->where('channel_id',$params['channel_id'])
				->orderRaw("rand()")
				->find();
		$od['api_key'] = $keys['key'];

		if($info['channel_id']==4){

		}elseif($info['channel_id']==3){

		}elseif($info['channel_id']==2){
			if(!$rs2[$params['server_code']] || $rs2[$params['server_code']]<=0){
				return ['code'=>159];
			}
		}else{
			//获取购买的服务数量
			$sl = $this->getCountNew($od);
			// print_r($sl);exit;
			if(!isset($sl['online']) || $sl['online']<=0){
				return ['code'=>159];
			}
		}
		//开启事务
        Db::startTrans();
        try {

        	$od['id'] = $id = Db::name('order_sms')->insertGetId($od);
        	if(!$id){
        		Db::rollback();
        		return ['code'=>141,'data'=>[]];
        	}
			$ldata = [];
			$ldata['out_order_id'] = $id;
			$ldata['order_type'] = 20;
			$ldata['payment_type'] = 40;
			$ldata['member_id'] = $od['member_id'];
			// $ldata['trade_number'] = $od['order_sn']."_".rand(100,999);
			$ldata['pay_amount'] = $od['pay_amount'];
			$ldata['payment_status'] = 10;
			$ldata['begin_time'] = date('Y-m-d H:i:s');
			$lid = OrderThirdPayLogLogic::init()->add($ldata);
			//付款
			$rs4 = MemberAccountLogic::init()->accountOut($od['order_sn'], 1);
			if(!$rs4){
				Db::rollback();

				return ['code'=>141,'data'=>[]];
			}
			//修改订单状态
			$rs5 = SmsOrderLogic::init()->orderProcess($od, 20);
			if(!$rs5){
				Db::rollback();
				return ['code'=>141,'data'=>[]];
			}

			//API购买
			$od['channel_type'] = $info['channel_type'];
			$rs6 = $this->getNumber($od);
			// echo 'aaa';
			if(isset($rs6['response']) && $rs6['response']!=1){
				Db::rollback();
				return ['code'=>152,'data'=>[]];
			}else{
				if(isset($rs6['tzid'])){
					if(!$rs6['tzid']){
						Db::rollback();
						return ['code'=>141,'data'=>[]];
					}
					$ud = [];
					// $od['phone'] = $ud['phone'] = $rs6['number'];
					$od['api_id'] = $ud['api_id'] = $rs6['tzid'];

					//这个交给定时器做吧
					// $rsod = SmsApiLogic::getPhone('get_phone', $od);
					// if(isset($rsod['response']) && $rsod['response']==1){
					// 	$od['phone'] = $ud['phone'] = $rsod['number'];
					// }

					Db::name('order_sms')
					->where('id', $od['id'])
					->update($ud);
				}else{
					$ud = [];
					$od['phone'] = $ud['phone'] = $rs6['number'];
					$od['api_id'] = $ud['api_id'] = $rs6['id'];
					Db::name('order_sms')
					->where('id', $od['id'])
					->update($ud);
				}

				
			}
		 	Db::commit();
        } catch (\Exception $e) {
            // print_r($e->getMessage());
            sys_log($e->getMessage(),'number.log');
            // 回滚事务
            Db::rollback();
            return ['code'=>141,'data'=>[]];
        }
		
		return ['code'=>0,'data'=>$od];
	}
	
	//轮巡获取短信【接口暂时不再使用】
	function getSms($order_sn='', $member_id=0){
		sys_log($order_sn.'#'.$member_id,'getSms.log');
		if(!$member_id){
			return ['code'=>402];
		}
		$od = Db::name('order_sms')
				->alias('os')
				->field('os.*, cc.channel_type')
                ->join('channel_country cc','cc.id=os.country_id')
				->where('os.order_sn', $order_sn)
				->where('os.member_id', $member_id)
				->find();
		if(!$od){
			return ['code'=>149,'data'=>[]];
		}
		if($od['status']!=20){
			return ['code'=>154,'data'=>[]];
		}
		//请求API
		$rs = SmsApiLogic::getSms('get_sms', $od);
		if($rs['response']==1 || $rs['response']==4){
			//短信获取成功，修改短信
			Db::name('order_sms')
				->where('id', $od['id'])
				->update(['sms_status'=>20,'sms_code'=>$rs['sms'],'sms_content'=>$rs['text']]);
				
			return ['code'=>0,'data'=>['sms'=>$rs['sms'],'content'=>$rs]];
		}else{
			return ['code'=>150,'data'=>['sms'=>$rs['sms'],'content'=>$rs]];
		}
	}

	//取消收到号码的订单
	function denial($order_sn='', $member_id=0){
		if(!$member_id){
			return ['code'=>402];
		}
		$od = Db::name('order_sms')
				->alias('os')
				->field('os.*, cc.channel_type')
                ->join('channel_country cc','cc.id=os.country_id')
				->where('os.order_sn', $order_sn)
				->where('os.member_id', $member_id)
				->find();
		
		if(!$od){
			return ['code'=>149,'data'=>[]];
		}
		if($od['status']!=20){
			return ['code'=>154,'data'=>[]];
		}
		if($od['denial_status']==20){
			return ['code'=>164,'data'=>[]];
		}
		//短信查询
		$rs = SmsApiLogic::getSms('get_sms', $od);
		sys_log(json_encode($rs),'getSms_user.log');
		//短信获取到了
		if(isset($rs['response'])){
			if($rs['response']==1 || $rs['response']==4){
				$tmp = [];
				$tmp['sms_code'] = isset($rs['sms'])?$rs['sms']:"";
				$tmp['sms_content'] = isset($rs['text'])?$rs['text']:'';
				$tmp['sms_status'] = 20;
				//短信获取成功了，修改短信状态为已经获取到了
				$rrst = Db::name('order_sms')
		        		->where('id', $od['id'])
		        		->update($tmp);
			}
		}


		//请求释放接口
		$rs1 = SmsApiLogic::denial('denial', $od);
		sys_log(json_encode($rs1),'denial_user.log');
		if(isset($rs1['response']) && $rs1['response']==1){
	        $rrs = Db::name('order_sms')
	        		->where('id', $od['id'])
	        		->update(['exp_time'=>date('Y-m-d H:i:s'),'denial_status'=>20,'denial_content'=>json_encode($rs1)]);
	        if(!$rrs){
				return ['code'=>151,'data'=>[]];
			}
			return ['code'=>0,'data'=>[]];
		}else{
			$rrs = Db::name('order_sms')
	        		->where('id', $od['id'])
	        		->update(['exp_time'=>date('Y-m-d H:i:s'),'denial_status'=>20]);

			return ['code'=>0,'data'=>[]];
		}
	}
	//加入黑名单
	function ban($order_sn='', $member_id=0){
		if(!$member_id){
			return ['code'=>402];
		}
		$od = Db::name('order_sms')
				->alias('os')
                ->field('os.*, cc.channel_type')
                ->join('channel_country cc','cc.id=os.country_id')
				->where('os.order_sn', $order_sn)
				->where('os.member_id', $member_id)
				->find();
		if(!$od){
			return ['code'=>149,'data'=>[]];
		}
		if($od['status']!=20){
			return ['code'=>154,'data'=>[]];
		}
		if($od['ban_status']==20){
			return ['code'=>165,'data'=>[]];
		}
		if($od['sms_status']==20){
			return ['code'=>163,'data'=>[]];
		}
		//短信查询
		$rs = SmsApiLogic::getSms('get_sms', $od);
		//短信获取到了
		if(isset($rs['response'])){
			if($rs['response']==1 || $rs['response']==4){
				$tmp = [];
				$tmp['sms_code'] = isset($rs['sms'])?$rs['sms']:"";
				$tmp['sms_content'] = isset($rs['text'])?$rs['text']:'';
				$tmp['sms_status'] = 20;

				$rrst = Db::name('order_sms')
		        		->where('id', $od['id'])
		        		->update($tmp);
		        return ['code'=>163,'data'=>[]];
			}elseif(isset($rs[0]) && $rs[0]['msg']!=null){
				$tmp = [];
				$tmp['sms_code'] = $rs[0]['msg'];
				$tmp['sms_content'] = $rs[0]['msg'];
				$tmp['sms_status'] = 20;

				$rrst = Db::name('order_sms')
		        		->where('id', $od['id'])
		        		->update($tmp);
		        return ['code'=>163,'data'=>[]];
			}
		}

		//请求加入黑名单接口
		$rs1 = SmsApiLogic::ban('ban', $od);
		sys_log(json_encode($rs1),'ban_user.log');
		if(isset($rs1['response']) && $rs1['response']==1){
	        $rrs = Db::name('order_sms')
	        		->where('id', $od['id'])
	        		->update(['ban_time'=>date('Y-m-d H:i:s'),'ban_status'=>20,'ban_content'=>json_encode($rs1)]);
	        if(!$rrs){
				return ['code'=>153,'data'=>[]];
			}
			return ['code'=>0,'data'=>[]];
		}else{
			$rrs = Db::name('order_sms')
	        		->where('id', $od['id'])
	        		->update(['ban_time'=>date('Y-m-d H:i:s'),'ban_status'=>20,'ban_content'=>json_encode($rs1)]);
	        return ['code'=>0,'data'=>[]];
		}
	}

	function getSmsList($lang='zh', $member_id=0, $exchange_rate=1){
		if(!$member_id){
			return ['code'=>402];
		}
		// echo 141502/60/60;
		$sjc = time();
		$page_size = input('page_size', 10);
		$parent_id = input('parent_id', 0);

		$start_time = input('start_time','');
		$end_time = input('end_time','');
		$start_time && $start_time = $start_time." 00:00:00";
		$end_time && $end_time = $end_time." 23:59:59";

		$status = input('status','');
		$ban_status = input('ban_status','');
		$denial_status = input('denial_status', '');
		$sms_status = input('sms_status', '');

		$phone = input('phone', '');
		$order_sn = input('order_sn', '');

		$fields = "os.id,
						 os.parent_id,
						 os.order_sn,
						 os.member_id,
						 os.phone,
						 os.api_id,
						 os.channel_id,
						 os.country_code,
						 os.server_code,
						 cc.country_ico,
						 cs.server_ico,
						 os.sms_code,
						 os.sms_content,
						 round(os.pay_amount / $exchange_rate,2) as pay_amount,
						 os.add_time,
						 os.pay_time,
						 os.status,
						 os.sms_status,
						 os.ban_status,
						 os.denial_status,
						 if(600 - ($sjc - unix_timestamp(os.pay_time))>0, 600 - ($sjc - unix_timestamp(os.pay_time)), 0) as sjc";
		if($lang=='zh'){
			$fields .=",cs.server_zh as server_name";
		}else{
			$fields .=",cs.server_en as server_name";
		}

		$obj = Db::name('order_sms')
				->alias('os')
				->field($fields)
				->join('channel_country cc','cc.id=os.country_id')
				->join('channel_server cs','cs.id=os.server_id')
				->where('member_id', $member_id);
		if($phone){
			$obj->where('phone', $phone);
		}
		if($order_sn){
			$obj->where('order_sn','like','%'.$order_sn.'%');
		}
		//订单支付状态
		if($status){
			$obj->where('os.status',$status);
		}else{
			$obj->where('os.status','<>', 40);
		}
		//短信接收状态
		if($sms_status){
			$obj->where('os.sms_status',$sms_status);
		}
		//拉黑状态
		if($ban_status){
			$obj->where('os.ban_status',$ban_status);
		}
		//释放状态
		if($denial_status){
			$obj->where('os.denial_status',$denial_status);
		}
		//开始时间
		if($start_time){
			$obj->where('os.add_time','>=', $start_time);
		}
		//结束时间
		if($end_time){
			$obj->where('os.add_time','<=', $end_time);
		}
		$obj->where('parent_id', $parent_id);
		$list = $obj->order('id desc')
				->paginate($page_size);
		return ['code'=>0,'data'=>$list];
	}

	//获取二次购买订单
	function getSecondSmsList($lang='zh', $member_id=0, $exchange_rate=1){
		if(!$member_id){
			return ['code'=>402];
		}
		// echo 141502/60/60;
		$sjc = time();


		$fields = "os.id,
						 os.parent_id,
						 os.order_sn,
						 os.member_id,
						 os.phone,
						 os.api_id,
						 os.channel_id,
						 os.country_code,
						 os.server_code,
						 cc.country_ico,
						 cs.server_ico,
						 os.sms_code,
						 os.sms_content,
						 round(os.pay_amount / $exchange_rate,2) as pay_amount,
						 os.add_time,
						 os.pay_time,
						 os.status,
						 os.sms_status,
						 os.ban_status,
						 os.denial_status,
						 if(600 - ($sjc - unix_timestamp(os.pay_time))>0, 600 - ($sjc - unix_timestamp(os.pay_time)), 0) as sjc";
		if($lang=='zh'){
			$fields .=",cs.server_zh as server_name";
		}else{
			$fields .=",cs.server_en as server_name";
		}

		$list = Db::name('order_sms')
				->alias('os')
				->field($fields)
				->join('channel_country cc','cc.id=os.country_id')
				->join('channel_server cs','cs.id=os.server_id')
				->where('member_id', $member_id)
				->where('os.status',20)
				->where('os.ban_status',10)
				->where('os.denial_status',10)
				->where('parent_id','>', 0)
				->order('id desc')
				->select();
		return ['code'=>0,'data'=>$list];
	}

	//关闭订单
	function closeOrder($order_sn='', $member_id=0){
		if(!$member_id){
			return ['code'=>402];
		}
		$od = Db::name('order_sms')
				->where('order_sn', $order_sn)
				->where('member_id', $member_id)
				->find();
		if(!$od){
			return ['code'=>149,'data'=>[]];
		}
		if($od['status']!=90 && $od['status']!=10){
			return ['code'=>154,'data'=>[]];
		}

		$rs = Db::name('order_sms')
				->where('id', $od['id'])
				->update(['status'=>40]);
		if($rs){
			return ['code'=>0];
		}else{
			return ['code'=>153,'data'=>['content'=>$rs]];
		}
	}

	//二次购买短信
	function buySmsSecond($lang='zh', $member_id=0){
		if(!$member_id){
			return ['code'=>402];
		}
		$order_sn = input('order_sn', '');
		if(!$order_sn){
			return ['code'=>155];
		}
		//判断下是否可以继续二次购买
		$ecin = Db::name('order_sms')
				->field('id')
				->where('parent_id','>',0)
				->where('status', 20)
				->where('sms_status', 10)
				->where('ban_status', 10)
				->where('denial_status', 10)
				->find();
		if($ecin){
			return ['code'=>162];
		}


		$info = Db::name('order_sms')
				->alias('os')
                ->field('os.*, cc.channel_type')
                ->join('channel_country cc','cc.id=os.country_id')
				->where('os.order_sn', $order_sn)
				->where('os.status', 20)
				->where('os.sms_status', 20)
				->where('os.ban_status', 10)
				->find();
		if(!$info){
			return ['code'=>160];
		}

		if($info['channel_id']==3){
			return ['code'=>168];
		}
		
		if($info['channel_id']!=2 && $info['channel_id']!=4){
			//判断号码是否准备好可以二次购买
			$redaySms = SmsApiLogic::getProverka('get_proverka', $info);
			if(isset($redaySms['response']) && $redaySms['response']!="ok"){
				return ['code'=>167,'msg'=>$redaySms['error_msg'],'data'=>[]];
			}
		}



		//开启事务
        Db::startTrans();
        try {
        	$od = $info;
        	$od['parent_id'] = $info['id'];
        	unset($od['id']);
        	unset($od['refund_time']);
        	unset($od['exp_time']);
        	unset($od['pay_time']);
        	unset($od['sms_code']);
        	unset($od['sms_content']);
        	unset($od['channel_type']);
        	unset($od['ban_content']);
        	unset($od['denial_content']);
        	$od['add_time'] = date('Y-m-d H:i:s');
        	$od['pay_time'] = date('Y-m-d H:i:s');
        	$od['sms_status'] = 10;
        	$od['status'] = 10;
        	$od['denial_status'] = 10;
        	$od['ban_status'] = 10;
        	$od['order_sn'] = CommonLogic::init()->createSn('order_sms','order_sn',14,'number','SO');
        	//新增二次购买订单
        	$od['id'] = $id = Db::name('order_sms')->insertGetId($od);
        	if(!$id){
        		Db::rollback();
        		return ['code'=>141,'data'=>[]];
        	}
			$ldata = [];
			$ldata['out_order_id'] = $id;
			$ldata['order_type'] = 20;
			$ldata['payment_type'] = 40;
			$ldata['member_id'] = $od['member_id'];
			// $ldata['trade_number'] = $od['order_sn']."_".rand(100,999);
			$ldata['pay_amount'] = $od['pay_amount'];
			$ldata['payment_status'] = 10;
			$ldata['begin_time'] = date('Y-m-d H:i:s');
			// $ldata['api_key'] = $od['api_key'];
			$lid = OrderThirdPayLogLogic::init()->add($ldata);
			//付款
			$rs4 = MemberAccountLogic::init()->accountOut($od['order_sn'], 1);
			if(!$rs4){
				Db::rollback();
				return ['code'=>141,'data'=>[]];
			}
			//修改订单状态
			$rs5 = SmsOrderLogic::init()->orderProcess($od, 20);
			if(!$rs5){
				Db::rollback();
				return ['code'=>141,'data'=>[]];
			}

			//API二次购买
			$od['channel_type'] = $info['channel_type'];
			$rs6 = SmsApiLogic::getNumberSecond('get_number_second', $od);
			sys_log(json_encode($rs6),'ercigoumai_user.log');
			// echo 'aaa';
		 	if(isset($rs6['response']) && $rs6['response']==1){
				Db::name('order_sms')->where('id', $id)->update(['api_id'=>$rs6['id']]);
				$od['api_id'] = $rs6['id'];
			}else{
				Db::rollback();
				return ['code'=>161,'msg'=>'号码正忙，30秒后再次尝试获取号码','data'=>[]];
			}

		 	Db::commit();

        } catch (\Exception $e) {
        	// print_r($e->getMessage());
            // 回滚事务
            Db::rollback();
            return ['code'=>141,'data'=>['msg'=>$e->getMessage()]];
        }

        return ['code'=>0,'data'=>$od];
	}


	//购买短信
	function getNumber($data=[]){
		// print_r($data);
		$rs = SmsApiLogic::getNumber('get_number', $data);
		return $rs;
	}
	//获取当前服务数量
	function getCountNew($data=[]){
		$rs = SmsApiLogic::getCountNew('get_count_new', $data);
		return $rs;
	}

}